package com.example.joblinker.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.joblinker.common.Result;
import com.example.joblinker.dto.ChatMessageDTO;
import com.example.joblinker.dto.JobDTO;
import com.example.joblinker.entity.User;
import com.example.joblinker.mapper.UserMapper;
import com.example.joblinker.service.ChatMessageService;
import com.example.joblinker.service.JobService;
import com.example.joblinker.util.JwtUtil;
import com.example.joblinker.vo.ChatMessageVO;
import com.example.joblinker.vo.JobVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/jobs")
public class JobController {

    @Resource
    private JobService jobService;

    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private UserMapper userMapper;
    
    @Resource
    private ChatMessageService chatMessageService;

    /**
     * 创建职位
     */
    @PostMapping
    public Result<JobVO> createJob(@RequestBody @Valid JobDTO jobDTO, HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        
        // 检查用户是否已关联公司
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }
        
        // 如果是招聘者但未关联公司，返回错误提示
        if (user.getRole() != null && user.getRole() == 1 && (user.getCompanyId() == null || user.getCompanyId() <= 0)) {
            return Result.error("请先添加公司信息", 10001);  // 使用特殊错误码，前端可根据此错误码跳转到公司信息填写页面
        }
        
        JobVO vo = jobService.createJob(jobDTO, userId);
        return Result.success(vo);
    }

    /**
     * 获取职位详情
     */
    @GetMapping("/{id}")
    public Result<JobVO> getJobDetail(@PathVariable Long id) {
        JobVO vo = jobService.getJobDetail(id);
        return Result.success(vo);
    }

    /**
     * 获取职位列表
     */
    @GetMapping
    public Result<Page<JobVO>> getJobList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<JobVO> voPage = jobService.getJobList(page, size);
        return Result.success(voPage);
    }

    /**
     * 搜索职位
     */
    @GetMapping("/search")
    public Result<Page<JobVO>> searchJobs(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<JobVO> voPage = jobService.searchJobs(keyword, categoryId, city, status, page, size);
        return Result.success(voPage);
    }

    /**
     * 更新职位信息
     */
    @PutMapping("/{id}")
    public Result<JobVO> updateJob(@PathVariable Long id, @RequestBody @Valid JobDTO jobDTO, HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        JobVO vo = jobService.updateJob(id, jobDTO, userId);
        return Result.success(vo);
    }

    /**
     * 更新职位状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateJobStatus(@PathVariable Long id, @RequestParam Integer status, HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        jobService.updateJobStatus(id, status, userId);
        return Result.success();
    }

    /**
     * 获取当前用户发布的职位
     */
    @GetMapping("/user")
    public Result<Page<JobVO>> getUserJobs(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        Page<JobVO> voPage = jobService.getUserJobs(userId, page, size);
        return Result.success(voPage);
    }
    
    /**
     * 获取职位聊天记录
     */
    @GetMapping("/{id}/chat")
    public Result<Page<ChatMessageVO>> getJobChatMessages(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "50") Integer size,
            HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        Page<ChatMessageVO> voPage = chatMessageService.getJobChatMessages(id, userId, page, size);
        return Result.success(voPage);
    }
    
    /**
     * 发送职位聊天消息
     */
    @PostMapping("/{id}/chat/send")
    public Result<ChatMessageVO> sendJobChatMessage(
            @PathVariable Long id,
            @RequestBody @Valid ChatMessageDTO messageDTO,
            HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        ChatMessageVO vo = chatMessageService.sendJobChatMessage(id, userId, messageDTO);
        return Result.success(vo);
    }
} 